package Cookie;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class CookieServlet extends BaseServlet {
    //创建cookie
    protected void createCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");
        //1.创建Cookie对象
        Cookie cook = new Cookie("key1", "value1");
        //2.通知客户端保存Cookie（只要是服务器通知客户端都是通过响应 resp 来实现）
        resp.addCookie(cook);
        Cookie cook1 = new Cookie("key2", "value2");
        resp.addCookie(cook1);
        resp.getWriter().write("Cookie创建成功");
    }

    //获取cookie
    protected void getCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");
        //只需要一行代码req.getCookies()--->Cookie[]
        Cookie[] cookies = req.getCookies();
        for (Cookie cookie : cookies) {
            String value = URLDecoder.decode(cookie.getName(), "UTF-8");
            resp.getWriter().write("Cookie[" + cookie.getName() + "=" + value + "] <br/>");
        }
        //遍历找到自己想要的cookie
        //使用URLEncoder.encode()编码 将中文等字符存进cookie
        Cookie cookie2 = reseachCookie.reseachCook("key2", cookies);
//        解码前输出
        resp.getWriter().write("2" + cookie2.getValue() + "<br/>");
        String decode1 = URLDecoder.decode(cookie2.getValue(), "utf-8");
        //        解码后输出
        resp.getWriter().write(decode1);
        resp.getWriter().write("<br/>" + cookie2.getName() + "=" + decode1 + "<br/>");
        resp.getWriter().write("<br>");
//       编码
        String value1 = URLEncoder.encode("陈俊华", "UTF-8");
//        解码前输出
        resp.getWriter().write(value1);
        resp.getWriter().write("<br>");
        Cookie cookie = new Cookie("key3",value1);
        resp.addCookie(cookie);
//        解码前输出
        resp.getWriter().write(cookie.getName()+"="+cookie.getValue());
        resp.getWriter().write("<br>");
//        解码后输出
        String decode = URLDecoder.decode(value1, "utf-8");
        resp.getWriter().write("<br>");
        resp.getWriter().write(decode);
        resp.getWriter().write("<br>");
        resp.getWriter().write(cookie.getName()+"="+decode);
    }

    //修改cookie
    protected void updateCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");
//       方案1：
//        创建一个同名的cookie对象,并赋予新值 调用resp中的addCookie添加
          //使用URLEncoder.encode()编码 将中文等字符存进cookie
        String newValue = URLEncoder.encode("陈俊华", "UTF-8");
        Cookie cookie = new Cookie("key2", newValue);
        resp.addCookie(cookie);
        resp.getWriter().write("cookie修改完毕");
        //方案2
        //找到要修改的cookie调用setValue()方法调用resp中的addCookie添加
//        Cookie[] cookies = req.getCookies();
//        Cookie cookie = reseachCookie.reseachCook("key2", cookies);
//        if(cookie!=null){
//            String newValue = URLEncoder.encode("陈俊华", "UTF-8");
//            cookie.setValue(newValue);
//            resp.addCookie(cookie);
//            resp.getWriter().write("cookie修改完毕");
//        }
//    }
    }
    //cookie的生命周期
    //生命控制指的是如果管理Cookie什么时候被销毁（删除）
//    setMaxAge()
//    正数表示在指定的秒数后过期
//    负数表示浏览器一关，Cookie就会被删除（默认）
//     0，表示马上删除

    protected void defaultLift(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            Cookie cookie = new Cookie("key5","value5");
            cookie.setMaxAge(-1);
            resp.addCookie(cookie);
    }
    protected void defaultOneHour(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("key6","value6");
        cookie.setMaxAge(60*60);
        resp.addCookie(cookie);
    }
        protected void deleteNow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");
        Cookie[] cookies = req.getCookies();
        Cookie cookie = reseachCookie.reseachCook("key5", cookies);
        if(cookie!=null){
            cookie.setMaxAge(0);
            resp.addCookie(cookie);
            resp.getWriter().write("删除成功");
        }
    }
    /**
     * Cookie有效路径 Path的设置
     * 可以有效的过滤哪些Cookie可以发送给服务器，哪些不发。
     * path属性是通过请求的地址来进行有效的过滤
     *
     */
    protected void CookiePath(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("key1","v1");
        cookie.setPath(req.getContextPath()+"/abc");
        resp.addCookie(cookie);
        resp.getWriter().write("123456");
        /**
         *
         *   CookieA  http://ip:port/工程路径
         *   CookieB  http://ip:port/工程路径/abc
         * http://ip:port/工程路径/a.html
         *  Cookie A 发送
         *  Cookie B 不发送
         * http://ip:port/工程路径/abc/a.html
         *  Cookie A 发送
         *  Cookie B 发送
         *
         */
    }


}

